package com.cmstop.bzgd.ui.adapter;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cmstop.bzgd.R;
import com.cmstop.bzgd.base.BaseActivity;
import com.cmstop.bzgd.beans.FProgram;
import com.cmstop.bzgd.ui.adapter.holder.LookBackNewsDetailHolder;
import com.cmstop.bzgd.ui.utils.GlideUtil;
import com.ruihang.generalibrary.utils.DateUtils;
import com.ruihang.generalibrary.utils.ListUtils;

import java.util.List;

public class LookBackNewsDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private OnItemClickListener itemClickListener;
    private List<FProgram> dataList;
    private BaseActivity activity;
    private Handler handler = new Handler();

    public LookBackNewsDetailAdapter(Context context, List<FProgram> list, OnItemClickListener itemClickListener) {
        this.context = context;
        this.itemClickListener = itemClickListener;
    }

    public BaseActivity getActivity() {
        return activity;
    }

    public void setActivity(BaseActivity activity) {
        this.activity = activity;
    }

    @Override
    public int getItemCount() {
        if (null != dataList) {
            return dataList.size();
        } else {
            return 0;
        }
    }

    /**
     * 变更数据中的标识
     * @param lastIndex
     * @param newIndex
     */
    public void updateDataStatus(int lastIndex, int newIndex) {
        if (dataList.size() > lastIndex && dataList.size() > newIndex) {
            dataList.get(lastIndex).setSelect(false);
            dataList.get(newIndex).setSelect(true);
        }
    }

    /**
     * 变更UI中的显示效果
     * @param lastHolder
     * @param newHolder
     */
    public void updateHolderStatus(LookBackNewsDetailHolder lastHolder, LookBackNewsDetailHolder newHolder) {
        if (null != lastHolder) {
            lastHolder.playIcon.setImageResource(R.mipmap.icon_home_play);
            lastHolder.newsTitle.setTextColor(context.getResources().getColor(R.color.colorBlack22));
            lastHolder.newsDate.setTextColor(context.getResources().getColor(R.color.colorGray98));
//            lastHolder.isPlaying.setVisibility(View.GONE);
        }
        if (null != newHolder) {
            newHolder.playIcon.setImageResource(R.mipmap.icon_home_onair);
            newHolder.newsTitle.setTextColor(context.getResources().getColor(R.color.colorTxRedMain));
            newHolder.newsDate.setTextColor(context.getResources().getColor(R.color.colorTxRedMain));
//            newHolder.isPlaying.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        View v = LayoutInflater.from(context).inflate(R.layout.item_look_back_detail, parent, false);
        holder = new LookBackNewsDetailHolder(v);
        return holder;
    }

    public void setData(List<FProgram> list) {
        if (null != list) {
            dataList = list;
            if (ListUtils.isListValued(dataList)) {
                dataList.get(0).setSelect(true);
            }
        }
        notifyDataSetChanged();
    }

    public void addData(List<FProgram> list) {
        int pos = getItemCount();
        if (null != list) {
            dataList.addAll(list);
        }
        if (pos > 0) {
            notifyItemRangeChanged(pos, getItemCount());
        }
    }

    public void updateData(List<FProgram> list) {
        if (null != list) {
            dataList = list;
        }
    }

    @Override
    public int getItemViewType(int position) {
        return dataList.get(position).getDataViewType();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final FProgram program = dataList.get(position);
        final LookBackNewsDetailHolder viewHolder = (LookBackNewsDetailHolder) holder;
        viewHolder.newsTitle.setText(program.getProgrammeTitle());

        if (!TextUtils.isEmpty(program.getPubTime())) {
            viewHolder.newsDate.setText(DateUtils.formatDate(DateUtils.getDataDate(program), ""));
        } else {
            viewHolder.newsDate.setText("");
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != itemClickListener) {
                    itemClickListener.OnItemClick(viewHolder, program, position);
                }
            }
        });
        viewHolder.newsTitle.setTextColor(context.getResources().getColor(R.color.colorBlack22));
        viewHolder.newsDate.setTextColor(context.getResources().getColor(R.color.colorGray98));
        viewHolder.isPlaying.setVisibility(View.GONE);
        if (!TextUtils.isEmpty(program.getProgrammeUrl())) {
            if (program.isSelect()) {
                viewHolder.newsTitle.setTextColor(context.getResources().getColor(R.color.colorTxRedMain));
                viewHolder.newsDate.setTextColor(context.getResources().getColor(R.color.colorTxRedMain));
//                viewHolder.isPlaying.setVisibility(View.VISIBLE);
                viewHolder.playIcon.setImageResource(R.mipmap.icon_home_onair);
            } else {
                viewHolder.playIcon.setImageResource(R.mipmap.icon_home_play);
            }
            viewHolder.playIcon.setVisibility(View.VISIBLE);
        } else {
            viewHolder.playIcon.setVisibility(View.GONE);
        }

        GlideUtil.loadImgSmall169Def(context, program.getProgrammeImage(), viewHolder.newsImage);
    }

    public interface OnItemClickListener {
        void OnItemClick(LookBackNewsDetailHolder viewHolder, FProgram item, int position);

    }
}
